package com.ruoyi.web.controller.applets;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.dto.Code2SessionDto;
import com.ruoyi.common.core.domain.dto.PhoneNumberDto;
import com.ruoyi.common.utils.applets.AppletsUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.task.domain.Task;
import com.ruoyi.wechat.domain.WechatUser;
import com.ruoyi.wechat.service.IWechatUserService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

import static com.ruoyi.common.constant.HttpStatus.LOGOUT;
import static com.ruoyi.common.constant.HttpStatus.RESOURCE_LIMIT;

/**
 * 小程序登录接口
 *
 * @author KevenPotter
 * @date 2024-04-26 10:15:15
 */
@RestController
@RequestMapping("/applets/login")
@AllArgsConstructor
public class AppletLoginController {

    private final ISysConfigService configService;
    private final ISysConfigService sysConfigService;
    private final TokenService tokenService;
    private final IWechatUserService wechatUserService;

    /**
     * 小程序默认登录接口
     *
     * @param httpServletRequest 前端请求
     * @return 返回登录结果
     * @author KevenPotter
     * @date 2024-04-26 15:47:18
     */
    @Anonymous
    @ResponseBody
    @GetMapping(value = "/login")
    public AjaxResult appletsLogin(HttpServletRequest httpServletRequest) {
        String appletsAppId = sysConfigService.getAppletsAppId();
        String appletsAppSecret = sysConfigService.getAppletsAppSecret();
        String code = httpServletRequest.getParameter("code");
        Code2SessionDto code2SessionDto = AppletsUtils.code2Session(appletsAppId, appletsAppSecret, code);
        return AjaxResult.success(code2SessionDto);
    }

    /**
     * 解密微信端加密的用户手机号码
     *
     * @param params 前端请求
     * @return 返回解密后微信端加密的用户手机号码
     * @author KevenPotter
     * @date 2024-04-26 15:47:25
     */
    @Anonymous
    @ResponseBody
    @PostMapping(value = "/decryptPhoneNumber")
    public AjaxResult decryptPhoneNumber(@RequestBody Map<String, String> params) {
        String appletsAppId = sysConfigService.getAppletsAppId();
        String iv = params.get("iv");
        String encryptedData = params.get("encryptedData");
        String sessionKey = params.get("sessionKey");
        String decrypt = AppletsUtils.decrypt(appletsAppId, encryptedData, sessionKey, iv);
        return AjaxResult.success(JSON.parseObject(decrypt, PhoneNumberDto.class));
    }

    /**
     * 获取小程序登录页登录信息
     *
     * @return 返回小程序登录页登录信息
     * @author KevenPotter
     * @date 2024-04-26 15:48:47
     */
    @Anonymous
    @ResponseBody
    @GetMapping(value = "/getLoginInfo")
    public AjaxResult getLoginInfo() {
        String appletsLoginLogo = configService.selectConfigByKey("applets_login_logo");
        String appletsIndexLogo = configService.selectConfigByKey("applets_index_logo");
        String appletsLoginSlogan = configService.selectConfigByKey("applets_login_slogan");
        String appletsSysName = configService.selectConfigByKey("applets_sys_name");
        String appletsIndexBackgroundImage = configService.selectConfigByKey("applets_index_background_image");
        String appletsIndexTips = configService.selectConfigByKey("applets_index_tips");
        Map<String, String> result = new HashMap<>();
        result.put("appletsLoginLogo", appletsLoginLogo);
        result.put("appletsIndexLogo", appletsIndexLogo);
        result.put("appletsLoginSlogan", appletsLoginSlogan);
        result.put("appletsSysName", appletsSysName);
        result.put("appletsIndexBackgroundImage", appletsIndexBackgroundImage);
        result.put("appletsIndexTips", appletsIndexTips);
        return AjaxResult.success(result);
    }
}
